home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 January: Mac OS SDK / Dev.CD Jan 00 SDK1.toast / Development Kits / Mac OS / Navigation Services SDK / Examples / Sampler / Sampler ƒ / event.c < prev    next >
Encoding:
Text File  |  1999-06-16  |  14.3 KB  |  549 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        event.c
  3.  
  4.     Copyright:    © 1997-1998 by Apple Computer, Inc., all rights reserved.
  5.  
  6. */
  7.  
  8. //    
  9. //    You may incorporate this sample code into your applications
  10. //    without restriction. This sample code has been provided "AS
  11. //    IS" and the responsibility for its operation is 100% yours.
  12. //    You are not permitted to redistribute the source as "Apple
  13. //    sample code" after having made changes. If you're going to
  14. //    re-distribute the source, we require that you make it clear
  15. //    in the source that the code was descended from Apple sample
  16. //    code, but that you've made changes.
  17. //    
  18.  
  19. #pragma segment AppSeg
  20.  
  21. #ifndef Common_Defs
  22. #include "Common.h"
  23. #endif
  24.  
  25. extern Document*    gFrontDocument;
  26. extern short         gQuit, gQuitting;
  27. extern short         gInBackground;
  28. extern Boolean        gCanDrag;
  29.  
  30. void DoContent(Document* theDocument, EventRecord* theEvent);
  31. void DoBackgroundContent(Document* theDocument, EventRecord* theEvent);
  32. void DoMouseDown(EventRecord* theEvent);
  33. void DoKey(char theChar);
  34. void DoKeyDown(EventRecord* theEvent);
  35. void DoActivate(EventRecord* theEvent);
  36. void DoUpdate(EventRecord* theEvent);
  37. void DoOSEvent(EventRecord* theEvent);
  38. void DoHighLevelEvent(EventRecord* theEvent);
  39. void DoEvent(EventRecord* theEvent);
  40. void DoIdle(void);
  41.  
  42. extern ControlActionUPP myActionProcUPP;    // for Mixed Mode
  43.  
  44. #define    SleepDuration        20        // WaitNextEvent() sleep constant
  45.  
  46. // *****************************************************************************
  47. // *
  48. // *    DoContent()
  49. // *
  50. // *    Handles mouseDown events in the content region of a document window.
  51. // *
  52. // *    (1)    If the mouseDown is on a control, handle the click by calling TrackControl.
  53. // *
  54. // *    (2)    If the mouseDown is on a draggable object (the document's hiliteRgn) and a
  55. // *        successful drag occurs, no further processing is necessary.
  56. // *
  57. // *    (3)    If the mouseDown is on a draggable object and the mouse is released without
  58. // *        dragging, set the insertion point to the original mouseDown location by calling
  59. // *        TEClick with the mouseDown information.
  60. // *
  61. // *    (4)    If the mouseDown is not on a draggable object and within the viewRect of the
  62. // *        TextEdit field, call TEClick to handle the mouseDown.
  63. // *
  64. // *****************************************************************************
  65. void DoContent(Document* theDocument, EventRecord* theEvent)
  66. {    
  67.     short            thePart;
  68.     Point            thePoint;
  69.     ControlHandle    theControl;
  70.  
  71.     if (theDocument->theTE != NULL)
  72.         {
  73.         SetPort(theDocument->theWindow);
  74.  
  75.         thePoint = theEvent->where;
  76.         GlobalToLocal(&thePoint);
  77.  
  78.         if (thePart = FindControl(thePoint,(WindowRef)theDocument->theWindow,&theControl))
  79.             {
  80.             if (theControl == theDocument->vScroll)
  81.                 {
  82.                 if (thePart == kControlIndicatorPart)
  83.                     {
  84.                     TrackControl(theControl,thePoint,0L);
  85.                     AdjustDocumentView(theDocument);
  86.                     }
  87.                 else
  88.                     TrackControl(theControl,thePoint,myActionProcUPP);
  89.  
  90.                 AdjustScrollBar(theDocument);
  91.                 }
  92.             }
  93.         else
  94.             if (PtInRgn(thePoint,theDocument->hiliteRgn))
  95.                 {
  96.                 if (gCanDrag)
  97.                     if (!DragText(theDocument,theEvent,theDocument->hiliteRgn))
  98.                         TEClick(thePoint,false,theDocument->theTE);
  99.                 }
  100.             else
  101.                 if (PtInRect(thePoint,&(**(theDocument->theTE)).viewRect))
  102.                     TEClick(thePoint,(theEvent->modifiers & shiftKey) != 0,theDocument->theTE);
  103.  
  104.         if (gCanDrag)
  105.             TEGetHiliteRgn(theDocument->hiliteRgn,theDocument->theTE);
  106.         }
  107. }
  108.  
  109.  
  110. // *****************************************************************************
  111. // *
  112. // *    DoBackgroundContent()
  113. // *
  114. // *    Handles mouseDown events in the content region of a document window
  115. // *    when the window is not frontmost. The following bullet items describe how this background
  116. // *    mouseDown event is handled:
  117. // *
  118. // *    (1)    If the mouseDown is not in a draggable object (not in the document's hiliteRgn) call
  119. // *        SelectWindow to bring the window to the front as usual.
  120. // *
  121. // *    (2)    If the mouseDown is in a draggable object and the mouse is released without
  122. // *        dragging, call SelectWindow when the mouse is released.
  123. // *
  124. // *    (3)    If the mouseDown is in a draggable object and a successful drag occurs, SelectWindow
  125. // *        should only be called if the drop occurred in the same window (the DragText function
  126. // *        calls SelectWindow in this case).
  127. // *
  128. // *****************************************************************************
  129. void DoBackgroundContent(Document* theDocument, EventRecord* theEvent)
  130. {    
  131.     Point    thePoint;
  132.  
  133.     SetPort((GrafPtr)theDocument->theWindow);
  134.  
  135.     thePoint = theEvent->where;
  136.     GlobalToLocal(&thePoint);
  137.  
  138.     if (PtInRgn(thePoint,theDocument->hiliteRgn))
  139.         {
  140.         if (!DragText(theDocument,theEvent,theDocument->hiliteRgn))
  141.             SelectWindow((WindowRef)theDocument->theWindow);
  142.          else
  143.             SelectWindow((WindowRef)theDocument->theWindow);
  144.         }
  145.     else
  146.         {
  147.         SelectWindow(theDocument->theWindow);
  148.         DoContent(theDocument,theEvent);
  149.         }
  150. }
  151.  
  152.  
  153. // *****************************************************************************
  154. // *
  155. // *    DoMouseDown()
  156. // *
  157. // *    (1)    If the mouseDown is not in a draggable object (not in the document's hiliteRgn) call
  158. // *        SelectWindow to bring the window to the front as usual.
  159. // *
  160. // *    (2)    If the mouseDown is in a draggable object and the mouse is released without
  161. // *        dragging, call SelectWindow when the mouse is released.
  162. // *
  163. // *    (3)    If the mouseDown is in a draggable object and a successful drag occurs, SelectWindow
  164. // *        should only be called if the drop occurred in the same window (the DragText function
  165. // *        calls SelectWindow in this case).
  166. // *
  167. // *****************************************************************************
  168. void DoMouseDown(EventRecord* theEvent)
  169. {    
  170.     short        thePart;
  171.     WindowPtr    theWindow;
  172.     Rect        dragRect;
  173.     Document*    theDocument;
  174.  
  175.     thePart = FindWindow(theEvent->where,&theWindow);
  176.     switch(thePart)
  177.         {
  178.         case inMenuBar:
  179.             AdjustMenus();
  180.             DoMenuCommand(MenuSelect(theEvent->where));
  181.             break;
  182.             
  183.         case inSysWindow:
  184.             SystemClick(theEvent,theWindow);
  185.             break;
  186.             
  187.         case inContent:
  188.             theDocument = IsDocumentWindow(theWindow);
  189.             if (theWindow == (WindowPtr)FrontWindow())
  190.                 DoContent(theDocument,theEvent);
  191.             else
  192.                 DoBackgroundContent(theDocument,theEvent);
  193.             break;
  194.             
  195.         case inDrag:
  196.             if (theWindow != (WindowPtr)FrontWindow())
  197.                 SelectWindow((WindowRef)theWindow);
  198.             dragRect = qd.screenBits.bounds;
  199.             DragWindow((WindowRef)theWindow,theEvent->where,&dragRect);
  200.             break;
  201.             
  202.         case inGrow:
  203.             if (theDocument = IsDocumentWindow(theWindow))
  204.                 {
  205.                 GrowDocumentWindow(theWindow,theEvent->where);
  206.                 if (theDocument->theTE != NULL)
  207.                     TEGetHiliteRgn(theDocument->hiliteRgn,theDocument->theTE);
  208.                 }
  209.             break;
  210.         
  211.         case inZoomIn:
  212.         case inZoomOut:
  213.             if (theDocument = IsDocumentWindow(theWindow))
  214.                 if ((TrackBox(theWindow,theEvent->where,thePart))&&(theDocument))
  215.                     DoZoomDocument(theDocument,theWindow,thePart);
  216.             break;
  217.  
  218.         case inGoAway:
  219.             if (theDocument = IsDocumentWindow(theWindow))
  220.                 if (TrackGoAway((WindowRef)theWindow,theEvent->where))
  221.                     {
  222.                     CloseDocument(theDocument,false);
  223.                     AdjustMenus();
  224.                     DrawMenuBar();
  225.                     }
  226.             break;
  227.         }
  228. }
  229.  
  230.  
  231. // *****************************************************************************
  232. // *
  233. // *    DoKey()
  234. // *
  235. // *    Called each time a character is typed on the keyboard to
  236. // *    be entered into a document window.
  237. // *
  238. // *****************************************************************************
  239. void DoKey(char theChar)
  240. {    
  241.     WindowPtr    theWindow;
  242.     Document*    theDocument;
  243.  
  244.     if (theWindow = (WindowPtr)FrontWindow())
  245.         {
  246.         if (theDocument = IsDocumentWindow(theWindow))
  247.             {
  248.             SetPort((GrafPtr)theDocument->theWindow);
  249.  
  250.             if (theDocument->theTE != NULL)
  251.                 {
  252.                 TEKey(theChar,theDocument->theTE);
  253.                 AdjustScrollBar(theDocument);
  254.                 theDocument->dirty = true;
  255.                 TEGetHiliteRgn(theDocument->hiliteRgn,theDocument->theTE);
  256.  
  257.                 if ((theChar < 0x1C) || (theChar > 0x1F))
  258.                     DisableUndoDrag();
  259.                 }
  260.             }
  261.         }
  262. }
  263.  
  264.  
  265. // *****************************************************************************
  266. // *
  267. // *    DoKeyDown()
  268. // *
  269. // *****************************************************************************
  270. void DoKeyDown(EventRecord* theEvent)
  271. {    
  272.     char theChar;
  273.  
  274.     theChar = theEvent->message & charCodeMask;
  275.  
  276.     if (theEvent->modifiers & cmdKey)
  277.         {
  278.         AdjustMenus();
  279.         DoMenuCommand(MenuKey(theChar));
  280.         }
  281.     else
  282.         DoKey(theChar);
  283. }
  284.  
  285.  
  286. // *****************************************************************************
  287. // *
  288. // *    DoActivate()
  289. // *
  290. // *****************************************************************************
  291. void DoActivate(EventRecord* theEvent)
  292. {    
  293.     WindowPtr    theWindow;
  294.     Document*    theDocument;
  295.  
  296.     if (theWindow = (WindowPtr)theEvent->message)
  297.         if (theDocument = IsDocumentWindow(theWindow))
  298.             DoActivateDocument(theDocument,(theEvent->modifiers & activeFlag));
  299. }
  300.  
  301.  
  302. // *****************************************************************************
  303. // *
  304. // *    DoUpdate()
  305. // *
  306. // *****************************************************************************
  307. void DoUpdate(EventRecord* theEvent)
  308. {    
  309.     Document* theDocument;
  310.  
  311.     if (theDocument = IsDocumentWindow((WindowPtr)theEvent->message))
  312.         UpdateWindow(theDocument);
  313. }
  314.  
  315.  
  316. // *****************************************************************************
  317. // *
  318. // *    DoOSEvent()
  319. // *
  320. // *****************************************************************************
  321. void DoOSEvent(EventRecord* theEvent)
  322. {    
  323.     Document* theDocument;
  324.  
  325.     switch ((theEvent->message >> 24) & 0x0FF)
  326.         {
  327.         case suspendResumeMessage:
  328.             gInBackground = (theEvent->message & resumeFlag) == 0;
  329.             if (theDocument = IsDocumentWindow((WindowPtr)FrontWindow()))
  330.                 DoActivateDocument(theDocument, !gInBackground);
  331.             break;
  332.         }
  333. }
  334.  
  335.  
  336. // *****************************************************************************
  337. // *
  338. // *    MyHandleOAPP()
  339. // *
  340. // *****************************************************************************
  341. pascal OSErr MyHandleOAPP(AppleEvent* /*theAppleEvent*/, AppleEvent* /*reply*/, long /*handlerRefCon*/)
  342. {
  343.     return noErr;
  344. }
  345.  
  346.  
  347. // *****************************************************************************
  348. // *
  349. // *    MyHandleODOC()
  350. // *
  351. // *****************************************************************************
  352. pascal OSErr MyHandleODOC(AppleEvent* theAppleEvent, AppleEvent* /*reply*/, long /*handlerRefCon*/)
  353. {    
  354.     AEDescList    docList;
  355.     AEKeyword    keyword;
  356.     DescType    returnedType;
  357.     FSSpec        theFSSpec;
  358.     Size        actualSize;
  359.     long        itemsInList;
  360.     short        index;
  361.     OSErr        result = noErr;
  362.     FInfo        fileInfo;
  363.  
  364.     if (result = AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&docList))
  365.         return result;
  366.  
  367.     if (result = AECountItems(&docList,&itemsInList))
  368.         return result;
  369.  
  370.     for (index=1;index<=itemsInList;index++)
  371.         {
  372.         if (result = AEGetNthPtr(&docList,index,typeFSS,&keyword,&returnedType,(Ptr)&theFSSpec,sizeof(FSSpec),&actualSize))
  373.             return result;
  374.  
  375.         // decide if the doc we are opening is a PICT or TEXT
  376.         result = FSpGetFInfo(&theFSSpec,&fileInfo);
  377.         if (result == noErr)
  378.             {
  379.             if (fileInfo.fdType == 'TEXT')
  380.                 DoOpenFile(&theFSSpec,false);
  381.             else
  382.                 DoOpenFile(&theFSSpec,true);
  383.             }
  384.         }
  385.     return result;
  386. }
  387.  
  388.  
  389. // *****************************************************************************
  390. // *
  391. // *    MyHandleQUIT()
  392. // *
  393. // *****************************************************************************
  394. pascal OSErr MyHandleQUIT(AppleEvent* /*theAppleEvent*/, AppleEvent* /*reply*/, long /*handlerRefCon*/)
  395. {    
  396.     Document* theDocument;
  397.  
  398.     gQuitting = true;
  399.     
  400.     while ((gQuitting) && (theDocument = IsDocumentWindow((WindowPtr)FrontWindow())))
  401.         CloseDocument(theDocument,true);
  402.     
  403.     if (gQuitting)
  404.         gQuit = true;
  405.  
  406.     return noErr;
  407. }
  408.  
  409.  
  410. // *****************************************************************************
  411. // *
  412. // *    ScrollProc()
  413. // *
  414. // *****************************************************************************
  415. pascal void ScrollProc(ControlHandle theControl, short theCode)
  416. {    
  417.     short        delta = 0, pageDelta;
  418.     Document*    theDocument;
  419.     Rect        viewRect;
  420.     SInt32         theCtrlRef = GetControlReference(theControl);
  421.  
  422.     if (theCode == 0)
  423.         return;
  424.  
  425.     theDocument = (Document*)theCtrlRef;
  426.     
  427.     viewRect = (**(theDocument->theTE)).viewRect;
  428.     pageDelta = ((viewRect.bottom - viewRect.top) / ScrollResolution) - 1;
  429.  
  430.     switch(theCode)
  431.         {
  432.         case kControlUpButtonPart:
  433.             delta = -1;
  434.             break;
  435.         case kControlDownButtonPart:
  436.             delta = 1;
  437.             break;
  438.         case kControlPageUpPart:
  439.             delta = -pageDelta;
  440.             break;
  441.         case kControlPageDownPart:
  442.             delta = pageDelta;
  443.             break;
  444.         }
  445.  
  446.     SetControlValue(theControl,GetControlValue(theControl) + delta);
  447.     AdjustDocumentView((Document*)theDocument);
  448. }
  449.  
  450.  
  451. // *****************************************************************************
  452. // *
  453. // *    DoHighLevelEvent()
  454. // *
  455. // *****************************************************************************
  456. void DoHighLevelEvent(EventRecord* theEvent)
  457. {
  458.     AEProcessAppleEvent(theEvent);
  459. }
  460.  
  461.  
  462. // *****************************************************************************
  463. // *
  464. // *    DoEvent()
  465. // *
  466. // *****************************************************************************
  467. void DoEvent(EventRecord* theEvent)
  468. {    
  469.     WindowPtr    theWindow;
  470.     Document*    theDocument;
  471.  
  472.     if (theWindow = (WindowPtr)FrontWindow())
  473.         if (theDocument = IsDocumentWindow(theWindow))
  474.             gFrontDocument = theDocument;
  475.  
  476.     switch(theEvent->what)
  477.         {
  478.         case mouseDown:
  479.             DoMouseDown(theEvent);
  480.             break;
  481.         case mouseUp:
  482.             break;
  483.         case keyDown:
  484.         case autoKey:
  485.             DoKeyDown(theEvent);
  486.             break;
  487.         case activateEvt:
  488.             DoActivate(theEvent);
  489.             break;
  490.         case updateEvt:
  491.             DoUpdate(theEvent);
  492.             break;
  493.         case osEvt:
  494.             DoOSEvent(theEvent);
  495.             break;
  496.         case kHighLevelEvent:
  497.             DoHighLevelEvent(theEvent);
  498.             break;
  499.         }
  500. }
  501.  
  502.  
  503. // *****************************************************************************
  504. // *
  505. // *    DoIdle()
  506. // *
  507. // *****************************************************************************
  508. void DoIdle()
  509. {    
  510.     WindowPtr    theWindow;
  511.     Document*    theDocument;
  512.  
  513.     if (theWindow = (WindowPtr)FrontWindow())
  514.         if (theDocument = IsDocumentWindow(theWindow))
  515.             if (theDocument->theTE != NULL)
  516.                 {
  517.                 SetPort((GrafPtr)theDocument->theWindow);
  518.                 TEIdle(theDocument->theTE);
  519.                 }
  520. }
  521.  
  522.  
  523. // *****************************************************************************
  524. // *
  525. // *    EventLoop()
  526. // *
  527. // *****************************************************************************
  528. void EventLoop()
  529. {    
  530.     short        gotEvent;
  531.     EventRecord    theEvent;
  532.     RgnHandle    theMouseRgn;
  533.  
  534.     theMouseRgn = NewRgn();
  535.     do     {
  536.         gotEvent = WaitNextEvent(everyEvent,&theEvent,SleepDuration,theMouseRgn);
  537.         if (gotEvent)
  538.             {
  539.             AdjustCursor(theEvent.where,theMouseRgn);
  540.             DoEvent(&theEvent);
  541.             AdjustCursor(theEvent.where,theMouseRgn);
  542.             }
  543.         else
  544.             DoIdle();
  545.         }
  546.     while (!gQuit);
  547.  
  548.     DisposeRgn(theMouseRgn);
  549. }